Insert spaces between wordsΒΆ
Insert spaces between words starting with capital letters.
import re
pattern = r"(\w)([A-Z])"
def capital_words_spaces(S):
# Replace a string with a part of itself
return re.sub(pattern, r"\1 \2", S)
# test
print(capital_words_spaces("Python"))
print(capital_words_spaces("PythonExercises"))
print(capital_words_spaces("PythonExercisesPracticeSolution"))
Output:
Python
Python Exercises
Python Exercises Practice Solution